package com.cbs.module.friend.friendmodule;

import android.content.Intent;
import com.cbs.module.friend.ModuleHandler;
import com.cbs.module.friend.entity.BlackListCache;
import com.cbs.module.friend.entity.FriendRelation;
import com.cbs.application.activity.CBSActivity;
import com.cbs.application.activity.CBSSimpleActivityResultHandler;
import com.cbs.applicationutils.Global;
import com.cbs.module.user.UserModule;
import com.cbs.module.user.network.CBSUserRequest;
import com.cbs.module.user.ui.UserModuleUI;
import com.cbs.module.user.ui.network.CBSUserUIRequest;
import com.cbs.network.Request;
import com.cbs.network.Response;
import com.cbs.network.ResponseHandler;
import com.cbs.runtimecache.RuntimeCache;

import java.net.HttpURLConnection;
import java.util.List;

/**
 * Created by broche on 1/16/16.
 */
public class BlackDelete {
    private final String BlackUrlTpl = "%s/wechat/api/v1/users/" + CBSUserRequest.Mark + "/blacklists/%d";
    private int blackId;
    private CBSActivity context;
    private ModuleHandler moduleHandler;
    private RuntimeCache runtimeCache;

    public BlackDelete(CBSActivity context, int blackId, ModuleHandler moduleHandler, RuntimeCache runtimeCache) {
        this.context = context;
        this.blackId = blackId;
        this.moduleHandler = moduleHandler;
        this.runtimeCache = runtimeCache;
    }

    public void start() {
        if (UserModule.getUser() == null || !UserModule.getUser().isRegistered()) {
            UserModuleUI.login(context, new CBSSimpleActivityResultHandler() {
                @Override
                public void onSuccess(int requestCode, Intent data) {
                    blackDeleteRequest();
                }

                @Override
                public void onCancel(int requestCode, Intent data) {
                    moduleHandler.onFailure(HttpURLConnection.HTTP_FORBIDDEN, "forbidden");
                }
            });
        } else {
            blackDeleteRequest();
        }
    }

    private void blackDeleteRequest() {
        Request request = new CBSUserUIRequest(context, String.format(BlackUrlTpl, Global.getServerHost(), blackId))
                .delete()
                .setResponseHandler(new ResponseHandler() {
                    @Override
                    public void onResponse(Response response) {
                        if (response.isSuccessful()) {
                            List<FriendRelation> blackList = (List<FriendRelation>) runtimeCache.getValue(BlackListCache.class);
                            boolean existed = false;
                            for (int i = 0; i < blackList.size(); i++) {
                                FriendRelation tblack = blackList.get(i);
                                if (tblack.getId() == blackId) {
                                    existed = true;
                                    blackList.remove(i);
                                    i--;
                                }
                            }
                            if (existed) {
                                runtimeCache.setValue(BlackListCache.class, blackList);
                            }
                            moduleHandler.onSuccess(response.getResult());
                        } else {
                            moduleHandler.onFailure(response.getCode(), response.getResult().toString());
                        }
                    }

                    @Override
                    public void onException(Request request, Exception e) {
                        moduleHandler.onException(e);
                    }
                });
        Global.getHttpClient().clone().send(request);
    }
}
